package com.mslinn.utils {
    import com.mslinn.yumifier.model.AppModel;
    
    import flash.display.DisplayObject;
    import flash.display.NativeWindow;
    import flash.display.NativeWindowDisplayState;
    import flash.events.NativeWindowBoundsEvent;
    import flash.filesystem.*;
    
    import mx.core.FlexGlobals;
    import mx.events.ResizeEvent;
    
    /** Handle persistent application state, per user.
    * @author <a href='mailto:mslinn@mslinn.com'>Mike Slinn</a> */
    public class AppState {
        private const debug:Boolean = false;
        private var iniFile:String;
        private var displayObject:DisplayObject;
        private var nativeWindow:NativeWindow;
        private var width:Number;
        private var height:Number;
        private var x:Number;
        private var y:Number;
        [Bindable] private var app:AppModel = AppModel.getInstance();
        
        public function AppState(displayObject:DisplayObject, iniFileName:String) { 
            this.displayObject = displayObject;
            this.nativeWindow = displayObject.stage.nativeWindow;
            this.iniFile = iniFileName;
            nativeWindow.addEventListener(NativeWindowBoundsEvent.RESIZE, function(event:NativeWindowBoundsEvent):void {
                // This gets called twice on close; the second time the dimensions are correct
                width = event.afterBounds.width;
                height = event.afterBounds.height;
                //trace("width", width, "height", height);
            });
            nativeWindow.addEventListener(NativeWindowBoundsEvent.MOVE, function(event:NativeWindowBoundsEvent):void {
                // This gets called twice on close; the second time the dimensions are correct
                x = nativeWindow.x;
                y = nativeWindow.y;
                //trace("x", x, "y", y);
            });
        }

        public function loadAppState():void {
            var properties:Properties = new Properties(File.applicationStorageDirectory.resolvePath(iniFile));
            if (properties.count==0)
                return;
            
            var strX:String = properties['x'];
            var strY:String = properties['y'];
            var strWidth:String = properties['width'];
            var strHeight:String = properties['height'];
            var strMaximized:String = properties['maximized'];
            var strServiceUrl:String = properties['serviceUrl'];
            var strUserId:String = properties['userId'];
            var strPassword:String = properties['password'];
            
            if (strX!=null && strX!="NaN")
                nativeWindow.x = x = Number(strX);
            if (strY!=null && strY!="NaN")
                nativeWindow.y = y = Number(strY);
            if (strWidth!=null && strWidth!="NaN")
                nativeWindow.width = width = Number(strWidth);
            if (strHeight!=null && strHeight!="NaN")
                nativeWindow.height = height = Number(strHeight);
                
            if (strMaximized=='true')
                nativeWindow.maximize();
            
            if (strServiceUrl!=null)
                app.serviceUrl = strServiceUrl;            
            if (strUserId!=null)
                app.userId = strUserId;
            if (strPassword!=null)
                app.password = strPassword;
        }
        
        /** Application does not persist the minimized state. */
        public function saveAppState():void {
            var fileStream:FileStream = new FileStream ();
            var file:File = File.applicationStorageDirectory.resolvePath(iniFile);
            fileStream.open(file, FileMode.WRITE);
        
            fileStream.writeUTFBytes("maximized=" + (nativeWindow.displayState==NativeWindowDisplayState.MAXIMIZED) + "\n"); 
            if (nativeWindow.displayState==NativeWindowDisplayState.MAXIMIZED || 
                nativeWindow.displayState==NativeWindowDisplayState.MINIMIZED)
                nativeWindow.restore();
        
            fileStream.writeUTFBytes("x=" + /*nativeWindow.*/x + "\n");
            fileStream.writeUTFBytes("y=" + /*nativeWindow.*/y + "\n");
            fileStream.writeUTFBytes("width=" + /*nativeWindow.*/width + "\n");
            fileStream.writeUTFBytes("height=" + /*nativeWindow.*/height + "\n"); 
            fileStream.writeUTFBytes("serviceUrl=" + app.serviceUrl + "\n"); 
            fileStream.writeUTFBytes("userId=" + app.userId + "\n"); 
            fileStream.writeUTFBytes("password=" + app.password + "\n"); 
            fileStream.close();
        }
    }
}